package it.softphone.commons.service.scripting.rhino;


import it.softphone.commons.json.server.JSONArray;
import it.softphone.commons.json.server.JSONConverter;
import it.softphone.commons.service.LogAware;

import org.mozilla.javascript.NativeArray;

public class NativeToJSONArrayConverter  implements JSONConverter<NativeArray>, LogAware{

	@Override
	public Object convert(@SuppressWarnings("rawtypes") Class type, Object value) {
		
		if( JSONArray.class.isAssignableFrom(type) && value instanceof NativeArray) {
			return this.convert((NativeArray) value);
		}
		return null;
	}

	@Override
	public Object convert(NativeArray value) {
		
		it.softphone.commons.json.server.JSONArray json = new it.softphone.commons.json.server.JSONArray();
		
		for( int i=0; i < value.getLength() ; ++i ) {
			
			final Object v = value.get(i, value);
			
			//try {
				json.put(v);
			//} catch (JSONException ex) {
			//	logger.warn( String.format("error set property [%d]!", i), ex );
			//}
		}
		
		return json;
	}

}
